package com.xzc.lease.web.admin.custom.config;

import com.xzc.lease.web.admin.custom.converter.StringToBaseEnumConverterFactory;
import com.xzc.lease.web.admin.custom.interceptor.AuthenticationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private StringToBaseEnumConverterFactory stringToBaseEnumConverterFactory;
    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;

    @Override
    public void addFormatters(FormatterRegistry registry) {
        log.info("注册String类型转换枚举对象转换器");
        //注册 string到枚举对象实例的转换
        registry.addConverterFactory(this.stringToBaseEnumConverterFactory);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("注册token拦截器");
        //注册拦截器，让拦截器生效。
        registry.addInterceptor(this.authenticationInterceptor)
                //拦截/admin下面的所有controller请求，**：表示任意一级任意内容
                .addPathPatterns("/admin/**")
                //排除 拦截登录相关的接口，否则用户不能登录和请求验证码
                .excludePathPatterns("/admin/login/**");
    }
}